<?php
/* SVN FILE: $Id: receipts.php 222 2009-09-16 20:07:22Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Viewing your receipts
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 222 $
* @lastrevision $Date: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_public/logs/receipts.php $
*/

class public_ibmarket_logs_receipts extends marketPublicCommand
{
	public function doExecute( ipsRegistry $registry )
	{
		if($this->memberData['member_id'] == 0 || $this->memberData['g_market_use'] == 0)
		{
			$this->registry->getClass('output')->showError('no_permission');
		}
		
		if(!isset($this->request['time']) || !isset($this->request['method']) || !in_array($this->request['method'], array('resell','buy')))
		{
			$this->receiptList();
		}
		else
		{
			$this->receiptDetails();
		}
	}
	
	private function receiptList()
	{
		$this->request['st'] = max(intval($this->request['st']), 0);
		
		$receipts = array();
		$this->DB->build(array(
								'select'	=> 'method, time_bought, SUM(quanity) total_quanity,SUM((price_bought+tax_total)*quanity) total_price,SUM(resell_price*quanity) total_resell',
								'from'		=> 'market_buy_logs',
								'calcRows'	=> TRUE,
								'where'		=> "member_id=".$this->memberData['member_id'],
								'order'		=> 'time_bought DESC',
								'group'		=> 'time_bought, method',
								'limit'		=> array($this->request['st'], 20),
						)	);
		$q = $this->DB->execute();
		$total = $this->DB->fetchCalculatedRows();
		while($receipt = $this->DB->fetch($q))
		{
			$receipts[] = $receipt;
		}
		
		$pages = $this->registry->output->generatePagination( array( 'totalItems'			=> $total,
																	 'itemsPerPage'			=> 20,
																	 'currentStartValue'	=> $this->request['st'],
																	 'baseUrl'				=> 'app=ibmarket&amp;module=logs&amp;section=receipts',
																	 'dotsSkip'				=> 5,
														)		);
		
		$content = $this->registry->getClass('output')->getTemplate('ibmarket')->receiptsList($receipts, $pages);
		
		$template = $this->registry->getClass('output')->getTemplate('ibmarket')->ibmarketHTMLWrapper($content);
		
		$this->registry->getClass('output')->setTitle( $this->settings['market_name'] );
		$this->registry->getClass('output')->addContent( $template );
		
        $this->registry->getClass('output')->sendOutput();
	}
	
	private function receiptDetails()
	{
		$this->request['time'] = intval($this->request['time']);
		
		$receipt = array(
							'method' => $this->request['method'],
							'time_bought' => $this->request['time'],
							'total_quanity' => 0,
							'total_price' => 0,
							'total_resell' => 0,
							'total_tax' => 0,
						);
		$items = array();
		
		$this->DB->build(array('select'=>'*','from'=>'market_buy_logs','where'=>"method='".$this->request['method']."' AND time_bought=".$this->request['time']." AND member_id=".$this->memberData['member_id']));
		$q = $this->DB->execute();
		while($item = $this->DB->fetch($q))
		{
			$item['total_price'] = numbers::multiply($item['quanity'],$item['price_bought']);
			$item['total_resell'] = numbers::multiply($item['quanity'],$item['resell_price']);
			$item['total_tax'] = numbers::multiply($item['quanity'],$item['tax_total']);
			$receipt['total_quanity'] = numbers::add($receipt['total_quanity'], $item['quanity']);
			$receipt['total_price'] = numbers::add($receipt['total_price'], $item['total_price']);
			$receipt['total_resell'] = numbers::add($receipt['total_resell'], $item['total_resell']);
			$receipt['total_tax'] = numbers::add($receipt['total_tax'], $item['total_tax']);
			$items[] = $item;
		}
		
		$receipt['final_price'] = numbers::add($receipt['total_price'], $receipt['total_tax']);
		
		$content = $this->registry->getClass('output')->getTemplate('ibmarket')->receiptDetails($receipt, $items);
		
		$template = $this->registry->getClass('output')->getTemplate('ibmarket')->ibmarketHTMLWrapper($content);
		
		$this->registry->getClass('output')->setTitle( $this->settings['market_name'] );
		$this->registry->getClass('output')->addContent( $template );
		
        $this->registry->getClass('output')->sendOutput();
	}
}